package org.tlipoca.smusersubscribe.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.genntii.smcommon.constant.GatewayConstant;
import org.genntii.smcommon.result.Result;
import org.genntii.smdomain.dto.UserSubscribeDTO;
import org.genntii.smdomain.entity.UserSubscribe;
import org.genntii.smdomain.vo.SubscribeNumVO;
import org.genntii.smdomain.vo.UserSubScribeClientVO;
import org.genntii.smdomain.vo.UserSubscribeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.tlipoca.smusersubscribe.service.UserSubscribeService;

import java.util.ArrayList;

@Tag(name = "/user_subscribe")
@Slf4j
@RestController
@RequestMapping("/user_subscribe")
public class UserSubscribeController {

    @Autowired
    private UserSubscribeService userSubscribeService;

    @Operation(summary = "获取用户关注用户列表")
    @GetMapping("/user/{userId}")
    public Result<ArrayList<UserSubscribeVO>> getUserSubscribeList(@PathVariable Long userId) {
        return Result.success(userSubscribeService.getUserSubscribeUserListService(userId));
    }

    @GetMapping("/article/subscribeChannel")
    public Result<ArrayList<UserSubScribeClientVO>> subscribe8c(@RequestParam("userId") Long userId){
        return Result.success(userSubscribeService.subscribe8c(userId));
    }

    @GetMapping("/article/subscribeUser")
    public Result<ArrayList<UserSubScribeClientVO>> subscribe8u(@RequestParam("userId")Long userId){
        return Result.success(userSubscribeService.subscribe8u(userId));
    }

    @Operation(summary = "获取用户关注频道列表")
    @GetMapping("/channel/{userId}")
    public Result<ArrayList<UserSubscribeVO>> getChannelSubscribeList(@PathVariable Long userId){
        ArrayList<UserSubscribeVO> userSubscribeChannelListService = userSubscribeService.getUserSubscribeChannelListService(userId);
        if (userSubscribeChannelListService==null||userSubscribeChannelListService.isEmpty()){
            return Result.success(new ArrayList<>());
        }
        return Result.success(userSubscribeChannelListService);
    }

    @Operation(summary = "用户关注")
    @PostMapping
    public Result userSubscribe(HttpServletRequest request, @RequestBody UserSubscribeDTO userSubscribeDTO) {
        Long userId = Long.valueOf(request.getHeader(GatewayConstant.USER_ID));
        userSubscribeService.userSubscribeServiceService(userId, userSubscribeDTO);
        return Result.success();
    }

    @Operation(summary = "获取用户关注以及粉丝数量")
    @GetMapping("/subscribeNum")
    public Result<SubscribeNumVO> getSubscribeNum(@RequestParam("userId") Long userId){
        return Result.success(userSubscribeService.getSubscribeNum(userId));
    }

    @Operation(summary = "用户取消关注")
    @DeleteMapping
    public Result userCancelSubscribe(HttpServletRequest request, @RequestBody UserSubscribeDTO userSubscribeDTO) {
        Long userId = Long.valueOf(request.getHeader(GatewayConstant.USER_ID));
        userSubscribeService.userCancelSubscribeService(userId, userSubscribeDTO);
        return Result.success();
    }

    // type: 1-频道 2-用户
    @Operation(summary = "获取用户是否关注")
    @GetMapping("/subscribed")
    public Result<Byte> userSubscribed(@RequestParam("type")Byte type, @RequestParam("targetId")Long targetId, @RequestParam("userId")Long userId){

        return Result.success(userSubscribeService.userSubscribed(type, targetId, userId));
    }

}
